package com.ssm.sport.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;

import java.io.File;

/**
 * Spring MVC 配置类
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Value("${file.upload.path:${user.home}/sport-mall-uploads}")
    private String uploadPath;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 添加外部上传文件夹的映射
        registry.addResourceHandler("/uploads/**")
                .addResourceLocations("file:" + uploadPath + File.separator);
        
        // 保留默认的静态资源映射
        registry.addResourceHandler("/static/**")
                .addResourceLocations("classpath:/static/");
    }

    /**
     * 添加视图控制器
     * 用于处理特殊URL重写规则
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        // 为订单详情添加RESTful风格的URL支持
        registry.addViewController("/order/detail/{orderNo}").setViewName("forward:/order/detail?orderNo={orderNo}");
    }
} 